(function (window) {
    function bindButtonHandler(instance, events = {}) {
        const defaultEvents = {
            'click': 'onClickButton',
            'post.before': 'postBefore',
            'post.after': 'postAfter',
            'form.confirm': 'formConfirm'
        }
        let mergeEvents = {...defaultEvents, ...events }
        function getButtonEvent(e) {
            if(mergeEvents[e] instanceof Function) return mergeEvents[e]
            if(!this[mergeEvents[e]]) return () => {}
            return this[mergeEvents[e]]
        }
        function onClickButton(btn, data={}) {
            // debug('ui.js', 'onClickButton', btn)
            if(btn.method == 'POST') {
                doPost(btn.methodObj, data)
            } else if(btn.method == 'FORM') {
                openForm(btn.methodObj, data)
            } else if(btn.method == 'MESSAGE_BOX') {
                openConfirm(btn.methodObj, data, btn)
            } else if(btn.method == 'EVENT') {
                onEvent(btn.methodObj, data)
            }
        }
        function doPost(url, data) {
            this._getButtonEvent('post.before')()
            post(url, data, (success, result) => {
                this._getButtonEvent('post.after')(success, result)
            })
        }
        function openForm(form, data ={}) {
            if(form.mode == 'DIALOG') {
                createComponent('SimpleFormDialog', {}, {
                    'confirm': this._getButtonEvent('form.confirm')
                }).open(form, {...data})
            }
        }
        function openConfirm(options, data, btn) {
            postData = data
            if(btn.submitFilterKey && btn.submitFilterDateKey) {
                postData = {}
                submitData = {}
                if(data instanceof Array) submitData = data.map(i => i[btn.submitFilterDateKey])
                else if(typeof {} == 'object') submitData = data[btn.submitFilterDateKey]
                else throw Error("未知数据类型:", data)
                postData[btn.submitFilterKey] = submitData
            }
            if(options.type == 'CONFIRM') {
                this.$Modal.confirm({
                    title: options.title,
                    content: options.content,
                    onOk: () => {
                        if(options.method == 'POST') {
                            doPost(options.url, postData)
                        }
                    },
                    onCancel: () => {
                    }
                })
            }
        }
        function onEvent(event, data) {
            const funcKey = 'event.'+event.name
            this._getButtonEvent(funcKey)(data)
        }
        instance['_getButtonEvent'] = getButtonEvent.bind(instance)
        instance[mergeEvents['click']] = onClickButton.bind(instance)
        doPost = doPost.bind(instance)
        openForm = openForm.bind(instance)
        openConfirm = openConfirm.bind(instance)
        onEvent = onEvent.bind(instance)
    }
    window.bindButtonHandler = bindButtonHandler
})(window)